#!/usr/bin/env ruby

require 'sqlite3'
require 'socket'
require 'message'
require 'rexml/document'

require 'squickSetupFunctions.rb'
require 'squickIrcCmds.rb'
require 'squickMessageHandlers.rb'
require 'squickInputThread.rb'
require 'squickTriggerHandler.rb'



class Squick

  # Sets up the bot, initialized the Database and triggers files, etc.
  def initialize(server, port, nick, dbName)
    @channel = "none"                       # The Channel name that the bot works in
    @nick = nick                            # The nick the bot should use

    @socket = TCPSocket.open(server, port)  # Open a socket connecting to the irc server
    initializeIrcConnection(@nick)

    @db = SQLite3::Database.new(dbName)     # Open up the database for all the bot's data

    loadAddons
  end

  # The main function of the bot.  You run this after the bot is initialized
  # and it will connect to the channel and do its thing.
  def run(logFile)
    # Start thread to listen for console commands
    listenerThread = Thread.new do inputThread() end

    #As long as the bot is connected to the IRC server...
    until @socket.eof? do
      # Grab the next message that the server has sent you
      msg = @socket.gets

      # Try to handle the message.  If successful, you're done :)
      next if(handleServerMessage(msg, logFile))

     # If nothing matched the message, then print out the raw data
     print("\e[1;35m#{msg}\e[0m")
    end

    # Join up the console command thread
    listenerThread.join
  end
end


#------------------------------------------------------------------------------
# Main executable!
#------------------------------------------------------------------------------

# Creating new bot
bot = Squick.new("irc.corp.google.com", 6667, 'squick', 'squick.db')
# Running the bot
bot.run("log.txt")

